﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Kernel.DBPersit.Dao.Util;

namespace HSDc.RingleFlow.Kernel.DBPersit.DbOrg.Dao
{
    internal class DepartmentDao
    {
        internal DepartmentDto GetDept(string deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var dept = (from d in entity.DepartmentDto
                       where (d.Dept_OrgID.ToLower() == deptId.ToLower())
                       select d).Single();
            return dept;
        }

        internal DepartmentDto GetDept(int deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var dept = (from d in entity.DepartmentDto
                        where (d.Dept_ID == deptId)
                        select d).Single();
            return dept;
        }

        internal EmployeeDto RetrieveDepartmentMainManager(int deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var emp = (from e in entity.EmployeeDto
                       join dm in entity.DeptManagerDto on e.Emp_ID equals dm.Manager_ID
                       where ((dm.Dept_ID == deptId) &&
                                  (dm.Main_Manager) &&
                                  (dm.Active))
                       select e).Single();
            return emp;
        }

        internal List<EmployeeDto> RetrieveDepartmentManagers(int deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var emp = from e in entity.EmployeeDto
                       join dm in entity.DeptManagerDto on e.Emp_ID equals dm.Manager_ID
                       where dm.Dept_ID == deptId
                       select e;
            return emp.ToList();
        }

        internal List<EmployeeDto> RetrieveDepartmentEmployees(int deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var emp = from e in entity.EmployeeDto
                      join ed in entity.EmpDeptDto on e.Emp_ID equals ed.Emp_ID
                      where ed.Belong_Dept_ID == deptId
                      select e;
            return emp.ToList();
        }

        internal DepartmentDto RetrieveDeptParent(int deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var parent = (from dept in entity.DepartmentDto
                          join dl in entity.DepartmentLevelDto on dept.Dept_ID equals dl.Parent_Dept_ID
                          where dl.Child_Dept_ID == deptId
                          select dept).Single();
            return parent;
        }

        internal List<DepartmentDto> RetrieveDeptChildren(int deptId)
        {
            RingleFlowEntities entity = ConnectionFactory.GetInstance().GetEntities();
            var child = from dept in entity.DepartmentDto
                          join dl in entity.DepartmentLevelDto on dept.Dept_ID equals dl.Child_Dept_ID
                          where dl.Parent_Dept_ID == deptId
                          select dept;
            return child.ToList();
        }
    }
}
